package com.iss.airsys.controller;

import com.iss.airsys.entity.User;
import com.iss.airsys.entity.Userinfo;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.model.UserWithRole;
import com.iss.airsys.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("user/")
public class UserController {

    @Autowired
    private UserService service;

//登陆时查看用户名密码是否已经存在
@RequestMapping("index")
public MyResponseData login(User user,HttpSession session,HttpServletRequest request,HttpServletResponse response){
    MyResponseData data = new MyResponseData();
    UserWithRole userWithRole = service.selectUser(user);
    session.setAttribute("loginfo",userWithRole);
    System.out.println(userWithRole.getR_id());
    if (userWithRole!=null){
        switch (userWithRole.getR_id()){
            case "r001":
                data.setErrorCode(1);
                data.setStatus("success");
                data.setObject(userWithRole);
                break;
            case "r002":
                data.setErrorCode(0);
                data.setStatus("success");
                data.setObject(userWithRole);
                break;
        }
    }else{
        data.setErrorCode(-1);
        data.setStatus("用户名密码错误!");
    }
    return data;
}
//把登陆的信息带入到首页
    @RequestMapping("loginfo")
    public MyResponseData loginfo(HttpSession session) {
        MyResponseData data = new MyResponseData();
        UserWithRole userWithRole = (UserWithRole)session.getAttribute("loginfo");
        data.setObject(userWithRole);
        System.out.println(userWithRole);
        return data;
    }
//注册输入用户名时,先查看用户名是否已经存在
    @RequestMapping("CheckUserName")
    public MyResponseData CheckUserName(String u_name){
        MyResponseData data = new MyResponseData();
            boolean flag = service.CheckUserName(u_name);
            System.out.println("flag="+flag);
            if(flag){
                data.setErrorCode(-1);
                data.setStatus("用户名已存在,请更还!");
            }else{
                data.setErrorCode(0);
            }
            return data;
    }
//注册的提交信息,成功后就刷新
    @RequestMapping("SubmitRegisterInfo")
    public MyResponseData SubmitRegisterInfo(@Param("u_name") String u_name, @Param("u_pwd") String u_pwd, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MyResponseData data = new MyResponseData();
        String u_id = UUID.randomUUID().toString();
        String u_roleid=request.getParameter("u_roleid");
        User user =new User(u_id,u_name,u_pwd,u_roleid);
        System.out.println("SubmitRegisterInfo=user===="+user);
        int count = service.SubmitRegisterInfo(user);
        if (count!=0){
            data.setErrorCode(0);
            data.setStatus("添加成功");
            data.setObject(user);
            response.sendRedirect("/gotoregister");
        }else {
            data.setErrorCode(-1);
            data.setStatus("添加失败");
        }
        return data;
    }


    @RequestMapping("forgetpwd")
    public MyResponseData forgetpwd(String uInfo_name,String uInfo_pwd,String uInfo_phone){
        //通过电话找出用户名,如果用户名相同就去改密码
        MyResponseData data = new MyResponseData();
        String uInfo_name_f=service.selectuserInfoByphone(uInfo_phone);
        System.out.println(uInfo_name_f+"========"+uInfo_pwd);
        if (uInfo_name.equals(uInfo_name_f)){

            int count = service.updateUser(uInfo_name,uInfo_pwd);
            if (count>0){
                data.setErrorCode(0);
                data.setStatus("修改成功");
            }else{
                data.setErrorCode(-1);
                data.setStatus("修改失败");
            }
        }else{
            data.setErrorCode(-1);
            data.setStatus("用户名和电话不匹配不能修改");
        }
        System.out.println(data);
        return data;

    }











}
